package top.likeqc.leetcode.editor.cn;

/**
 * @see <a
 *     href="https://leetcode.cn/problems/minimum-number-of-operations-to-move-all-balls-to-each-box">leetcode题库</a>
 */
public class MinimumNumberOfOperationsToMoveAllBallsToEachBox {
    public static void main(String[] args) {
        Solution solution = new MinimumNumberOfOperationsToMoveAllBallsToEachBox().new Solution();
        System.out.println(solution.minOperations("110").toString());
    }
    // leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] minOperations(String boxes) {
            int n = boxes.length();
            int[] ans = new int[n];
            int left = 0, right = 0;

            for (int i = 1; i < n; i++) {
                if (boxes.charAt(i) == '1') {
                    ans[0] += Math.abs(i);
                    right++;
                }
            }

            left = boxes.charAt(0) == '0' ? 0 : 1;

            for (int i = 1; i < n; i++) {
                ans[i] = ans[i - 1] + left - right;
                if (boxes.charAt(i) == '1') {
                    left++;
                    right--;
                }
            }
            return ans;
        }
    }
    // leetcode submit region end(Prohibit modification and deletion)

}
